#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
const long long PIN=1e18;
const int N=500003,M=(int)1e6+3;
int n,m,b[N],cnt[M]; long long res[M];
vector<int> a[N],all1,all2,sub[M],num;
int binary(const vector<int>& alls,int x){
	int l=0,r=alls.size();
	while(l<r){
		int mid=(l+r)/2;
		(alls[mid]>=x)?(r=mid):(l=mid+1);
	}
	return r;
}
int read(){
	char ch; int x=0;
	do ch=getchar();
	while(ch<'0'||ch>'9');
	while(ch>='0'&&ch<='9')
		x=x*10+(ch-'0'),ch=getchar();
	return x;
}
void write(long long x,char ch){
	char a[24]; int n=0,i;
	do a[++n]=x%10+'0',x/=10; while(x>0);
	for(i=n;i>0;i--) putchar(a[i]);
	putchar(ch);
}
int main(){
	freopen("list.in","r",stdin);
	freopen("list.out","w",stdout);
	int i,j,s1,la; long long s2=0,ans=PIN;
	for(n=read(),i=1;i<=n;i++){
		s1=read(),a[i].resize(s1);
		for(j=0;j<s1;j++) a[i][j]=read();
	}
	for(m=read(),i=1;i<=n;i++) b[i]=read();
	for(all1.push_back(1),i=1;i<=n;i++)
		for(j=1;j<a[i].size();j++)
			all1.push_back(a[i][j]-a[i][j-1]+1);
	for(all2.push_back(1),i=1;i<=n;i++)
		for(j=0;j<a[i].size();j++)
			all2.push_back(a[i][j]);
	sort(all1.begin(),all1.end());
	sort(all2.begin(),all2.end());
	all1.erase(unique(all1.begin(),all1.end()),all1.end());
	all2.erase(unique(all2.begin(),all2.end()),all2.end());
	for(i=1;i<=n;i++)
		for(j=0;j<a[i].size();j++){
			s1=binary(all2,a[i][j]),cnt[s1]++;
			if(j) sub[binary(all1,a[i][j]-a[i][j-1]+1)].push_back(s1);
		}
	for(i=1,s1=0;i<=n;i++) s1+=a[i].size();
	for(la=0,i=0;i<all1.size();la=all1[i++]){
		res[i]=(i?res[i-1]:0ll)+(long long)s1*(all1[i]-la-1);
		s1-=sub[i].size(),res[i]+=s1;
	}
	for(i=0;i<all1.size();i++) res[i]*=m;
	for(i=0;i<all2.size();i++)
		s2+=(long long)b[cnt[i]]*cnt[i];
	for(i=0;i<all1.size();res[i++]+=s2)
		for(j=0;j<sub[i].size();j++){
			s2-=(long long)b[cnt[sub[i][j]]]*cnt[sub[i][j]],cnt[sub[i][j]]--;
			s2+=(long long)b[cnt[sub[i][j]]]*cnt[sub[i][j]];
		}
	for(i=0;i<all1.size();i++) ans=min(ans,res[i]);
	for(i=0;i<all1.size();i++)
		if(res[i]==ans) num.push_back(all1[i]);
	write(ans,' '),write(num.size(),'\n');
	for(i=0;i<num.size();i++) write(num[i],' ');
	fclose(stdin);
	fclose(stdout);
	return 0;
}
